package com.bodacn.keypoint;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;



public class FragmentA extends Fragment implements View.OnClickListener {
    public static String TAG=FragmentA.class.getName();
    public static final int msgFocusBarcode = 1001;
    public static final int MSG_SCANBARCODE = 9001;
    public static final int MSG_QUERYMARK = 9002;
    public static final int MSG_CUTLIST = 9003;
    public static final int MSG_ERRMSG = 9004;
    //用于将Activity中的参数传给Fragment
    private Map<String,Object> fragmentParams=new HashMap<String,Object>();
    //用于与主Activity通讯;
    private Handler mainHandler;
    //全局变量
    Toast curToast;
    String curLoginUserNo,curLoginUser,curServiceUrl="",curServiceDir="",curDepartNo,curDepart,CPUID;
    Button btn_find,btn_CutQuery,btn_PullAdd,btn_PullEdit;
    EditText edt_barcode;
    ImageButton btn_editBarcode;
    ListView markListView,cutListView;
    TextView txt_HintInfo,txt_OrderNo,txt_StyleNo,txt_StyleSort;
    JSONArray headJSON,detailJSON,cutlistJson;
    TextView itemSize1,itemSize2,itemSize3,itemSize4,itemSize5,itemSize6,itemSize7,itemSize8,itemSize9,itemSize10,itemSize11,itemSize12;
    String curProduceCutMaID,curProduceMasterID,curMarkDeID,curMakeState;//裁床ID,马克ID
    private ProgressDialog waitDialog = null;
    //判断是否为数字，不然不转
    public static boolean isNumeric(String str){
        try {
            new BigDecimal(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    public FragmentA() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //得到Activity的Handler,用于发消息给主Activity;
        MainActivity activity=(MainActivity) getActivity();
        mainHandler=activity.mainHandler;
        //加接口监听
        activity.setTransParamsListenerA(new MainActivity.OnTransParamsListenerA() {
            @Override
            public void TransParams(Map<String, Object> mParams) {
                if (mParams!=null) {
                    try {
                        String paramComm= mParams.get("ParamComm").toString();
                        if (paramComm=="LoginParam")
                        {
                            Log.d(this.getClass().getName(),"LoginParam------------------>1");
                            //传递登陆参数信息;
                            curLoginUserNo=mParams.get("LoginUserNo").toString();
                            curLoginUser=mParams.get("LoginUser").toString();
                            curServiceUrl=mParams.get("ServiceUrl").toString();
                            curServiceDir=mParams.get("ServiceDir").toString();
                            curDepartNo=mParams.get("DepartNo").toString();
                            curDepart=mParams.get("Depart").toString();
                            CPUID=mParams.get("CPUID").toString();
                            Log.d(this.getClass().getName(),"LoginParam------------------>2");
                            //发消息置焦点
                            Message msg =new Message();
                            msg.what = msgFocusBarcode;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }



    /**
     * 定义一个Handler用于接收Fragment发出来的指令
     */
    public Handler fragmentHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg!=null){
                //任意消息来均置焦点
                if (edt_barcode.isFocused()==false) setEditBarcodeFocus();
                else edt_barcode.selectAll();
                //消息处理;
                System.out.println("收到的消息是："+msg.what);
                int msgId = msg.what;
                switch (msgId) {
                    case MSG_ERRMSG:
                        //提示提交完成，需确认;
                        Bundle bundle1=msg.getData();
                        String resultMsg=bundle1.getString("Hint");
                        // 创建对话框
                        AlertDialog alertDialog = new AlertDialog.Builder(getContext())
                                .setTitle("系统提示").setMessage(resultMsg)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                    }
                                }).create();
                        alertDialog.show(); // 显示对话框
                        break;
                    case MSG_SCANBARCODE:
                        fillFormtext();
                        break;
                    case MSG_QUERYMARK:
                        final MarkListAdapter markListAdapter = new MarkListAdapter(getActivity());
                        markListView.setAdapter(markListAdapter);
                        break;
                    case MSG_CUTLIST:
                        final CutListAdapter cutListAdapter = new CutListAdapter(getActivity());
                        cutListView.setAdapter(cutListAdapter);
                        break;
                    default:
                        break;
                }
            }
        }
    };



    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏状态栏和底部虚拟按键
        View decorView = getActivity().getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(flag);
        }
    }
    //设置焦点到扫描输入框
    private void setEditBarcodeFocus()
    {
        //设置焦点
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.Start");
        if (edt_barcode!=null) {
            edt_barcode.selectAll();
            edt_barcode.setFocusable(true);
            edt_barcode.setFocusableInTouchMode(true);
            edt_barcode.requestFocus();
            edt_barcode.requestFocusFromTouch();
            hideIM(getView());
            hideBottomUIMenu();
        }
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.End");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_a, container, false);
        InitView(root);
        System.out.println(this.getClass().getName()+"_onCreateView");
        return root;
    }
    //初始化;
    public void InitView(View view){
        //提示框pd=new ProgressDialog(this);
        waitDialog= new ProgressDialog(getContext());
        waitDialog.setTitle(getString(R.string.SystemHint));
        //界面控件;
        markListView= view.findViewById(R.id.markListView);
        markListView.setFocusable(false);
        markListView.setFocusableInTouchMode(false);
        markListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取ID
                if (detailJSON.optJSONObject(position).has("MarkDeID")==true)
                {
                    try {
                        curMarkDeID=detailJSON.optJSONObject(position).getString("MarkDeID");
                        showCutDetail(curProduceMasterID,curMarkDeID);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        cutListView= view.findViewById(R.id.cutListView);
        cutListView.setFocusable(false);
        cutListView.setFocusableInTouchMode(false);
        cutListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取ID
                if (cutlistJson.optJSONObject(position).has("ProduceCutMaID")==true)
                {
                    try {
                        curProduceCutMaID=cutlistJson.optJSONObject(position).getString("ProduceCutMaID");
                        if (cutlistJson.optJSONObject(position).has("MakeState")==true) curMakeState=cutlistJson.optJSONObject(position).getString("MakeState");
                        else curMakeState="";
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        btn_CutQuery=view.findViewById(R.id.btn_CutQuery);
        btn_CutQuery.setOnClickListener(this);
        btn_find=view.findViewById(R.id.btn_find);
        btn_find.setOnClickListener(this);
        btn_PullAdd=view.findViewById(R.id.btn_PullAdd);
        btn_PullAdd.setOnClickListener(this);
        btn_PullEdit=view.findViewById(R.id.btn_PullEdit);
        btn_PullEdit.setOnClickListener(this);
        btn_editBarcode=view.findViewById(R.id.btn_editBarcode);
        btn_editBarcode.setOnClickListener(this);
        btn_editBarcode.setFocusable(false);
        btn_editBarcode.setFocusableInTouchMode(false);
        //界面Edit;
        edt_barcode= view.findViewById(R.id.edt_barcode);
        edt_barcode.setInputType(InputType.TYPE_NULL);
        edt_barcode.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event!=null){
                    if ((event.getAction()==KeyEvent.ACTION_DOWN)&&(event.getKeyCode()== KeyEvent.KEYCODE_ENTER))
                    {
                        //虚拟键盘或实体键盘可考虑是否通过deviceID，或source来判断
                        // KeyEvent:KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ENTER, scanCode=0, metaState=0, flags=0x16, repeatCount=0, eventTime=142515176, downTime=142515176, deviceId=-1, source=0x0 }
                        //隐藏键盘
                        hideIM(v);
                        if (edt_barcode.getText().length()==0) {
                            curToast.setText(getString(R.string.PleaseInputBarcode));
                            curToast.show();
                        } else {
                            searchBarcodeData(edt_barcode.getText().toString());
                        }
                        //未有焦点
                        if (edt_barcode.isFocused()==false){
                            //发消息置焦点
                            Message msg =new Message();
                            msg.what = msgFocusBarcode;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        } else edt_barcode.selectAll();
                    }

                }
                return false;
            }
        });
        edt_barcode.requestFocus();
        edt_barcode.requestFocusFromTouch();
        txt_HintInfo=view.findViewById(R.id.txt_HintInfo);
        txt_OrderNo=view.findViewById(R.id.txt_OrderNo);
        txt_StyleNo=view.findViewById(R.id.txt_StyleNo);
        txt_StyleSort=view.findViewById(R.id.txt_StyleSort);
        //表格标题
        itemSize1=view.findViewById(R.id.txt_item_Size1);
        itemSize2=view.findViewById(R.id.txt_item_Size2);
        itemSize3=view.findViewById(R.id.txt_item_Size3);
        itemSize4=view.findViewById(R.id.txt_item_Size4);
        itemSize5=view.findViewById(R.id.txt_item_Size5);
        itemSize6=view.findViewById(R.id.txt_item_Size6);
        itemSize7=view.findViewById(R.id.txt_item_Size7);
        itemSize8=view.findViewById(R.id.txt_item_Size8);
        itemSize9=view.findViewById(R.id.txt_item_Size9);
        itemSize10=view.findViewById(R.id.txt_item_Size10);
        itemSize11=view.findViewById(R.id.txt_item_Size11);
        itemSize12=view.findViewById(R.id.txt_item_Size12);
        //初始提醒;
        curToast = Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT);

    }


    //插放音乐;
    private void playRingtone(){
        Uri notification = Uri.parse("android.resource://"+getActivity().getPackageName()+"/"+R.raw.scan);
        Ringtone r = RingtoneManager.getRingtone(getActivity(), notification);
        r.play();

    }
    //刷新缸布列表;
    private void searchBarcodeData(String BarcodeParam)
    {
        playRingtone();
        clearFormText();
        try {
            detailJSON=new JSONArray("[]");
            curProduceMasterID="";
            curMarkDeID="";
            curProduceCutMaID ="";
            //显示预排单;
            final MarkListAdapter markListAdapter = new MarkListAdapter(getActivity());
            markListView.setAdapter(markListAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //执行SQL过程
        String SqlComm = "Exec KeyPoint_MarkBarcode @Barcode='" + BarcodeParam + "',@OPERTYPE='SCANBARCODE',@UserNo='"+curLoginUserNo+ "',@CPUID='"+CPUID+"'";
        ReadSQLData(SqlComm);
    }

    //刷新缸布列表;
    private void queryMarkData(String OrderNoParam,String StyleNoParam,String StyleSortParam)
    {
        try {
            cutlistJson=new JSONArray("[]");
            curMarkDeID="";
            curProduceCutMaID ="";
            //显示预排单;
            final CutListAdapter cutListAdapter = new CutListAdapter(getActivity());
            cutListView.setAdapter(cutListAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //执行SQL过程
        String SqlComm = "Exec KeyPoint_MarkQuery @OrderNo='" + OrderNoParam+"',@StyleNo='" + StyleNoParam+"',@StyleSort='" + StyleSortParam + "',@OPERTYPE='QUERYMARK',@UserNo='"+curLoginUserNo+ "',@CPUID='"+CPUID+"'";
        ReadSQLData(SqlComm);
    }

    private void showCutDetail(String paramProduceMasterID,String paramMarkDeID){
        waitDialog.setTitle("获取床次清单..");
        curProduceCutMaID ="";
        //执行SQL过程
        String SqlComm = "Exec KeyPoint_GetCutList @ProduceMasterID='" + paramProduceMasterID+"',@MarkDeID='" + paramMarkDeID+ "',@OPERTYPE='CUTLIST',@UserNo='"+curLoginUserNo+ "',@CPUID='"+CPUID+"'";
        ReadSQLData(SqlComm);

    }
    // 隐藏手机键盘
    private void hideIM(View edt) {
        try {
            InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
            IBinder windowToken = edt.getWindowToken();
            if (windowToken != null) {
                im.hideSoftInputFromWindow(windowToken, 0);
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onClick(View v) {
        hideIM(v);
        Bundle paramBundle = new Bundle();
        switch (v.getId()){
            case R.id.btn_find:
                if (edt_barcode.getText().length()==0) {
                    curToast.setText(getString(R.string.PleaseInputBarcode));
                    curToast.show();
                } else //执行查询;
                {
                    searchBarcodeData(edt_barcode.getText().toString());
                }
                break;
            case R.id.btn_CutQuery:
                Intent intentCutQueryActivity=new Intent(getContext(),CutQueryActivity.class);
                //简单参数通过Intent的Extras传递;
                if((curProduceMasterID==null)||(curProduceMasterID.length()==0)) {
                    curToast.setText("请先刷马克条码过滤出合约信息!");
                    curToast.show();
                    return;
                }
                paramBundle.clear();
                paramBundle.putString("LoginUserNo", curLoginUserNo);
                paramBundle.putString("LoginUser",curLoginUser);
                paramBundle.putString("CurDepartNo",curDepartNo);
                paramBundle.putString("CurDepart",curDepart);
                paramBundle.putString("ServiceUrl",curServiceUrl);
                paramBundle.putString("ServiceDir",curServiceDir);
                paramBundle.putString("CPUID",CPUID);
                paramBundle.putString("ProduceMasterID", curProduceMasterID);
                paramBundle.putString("OrderNo",txt_OrderNo.getText().toString());
                paramBundle.putString("StyleNo",txt_StyleNo.getText().toString());
                paramBundle.putString("StyleSort",txt_StyleSort.getText().toString());
                paramBundle.putString("Size1",itemSize1.getText().toString());
                paramBundle.putString("Size2",itemSize2.getText().toString());
                paramBundle.putString("Size3",itemSize3.getText().toString());
                paramBundle.putString("Size4",itemSize4.getText().toString());
                paramBundle.putString("Size5",itemSize5.getText().toString());
                paramBundle.putString("Size6",itemSize6.getText().toString());
                paramBundle.putString("Size7",itemSize7.getText().toString());
                paramBundle.putString("Size8",itemSize8.getText().toString());
                paramBundle.putString("Size9",itemSize9.getText().toString());
                paramBundle.putString("Size10",itemSize10.getText().toString());
                paramBundle.putString("Size11",itemSize11.getText().toString());
                paramBundle.putString("Size12",itemSize12.getText().toString());
                intentCutQueryActivity.putExtras(paramBundle);
                startActivity(intentCutQueryActivity);
                break;
            case R.id.btn_PullAdd:
                hideIM(v);
                if ((detailJSON!=null)&&(detailJSON.length()>0)){
                    if (curMarkDeID.length()==0) {
                        curToast.setText("请选择马克计划!");
                        curToast.show();
                        return;
                    }
                    Intent intentPullActivity=new Intent(getContext(),PullActivity.class);
                    curProduceCutMaID = UUID.randomUUID().toString();
                    //简单参数通过Intent的Extras传递;
                    paramBundle.clear();
                    paramBundle.putString("LoginUserNo", curLoginUserNo);
                    paramBundle.putString("LoginUser",curLoginUser);
                    paramBundle.putString("CurDepartNo",curDepartNo);
                    paramBundle.putString("CurDepart",curDepart);
                    paramBundle.putString("ServiceUrl",curServiceUrl);
                    paramBundle.putString("ServiceDir",curServiceDir);
                    paramBundle.putString("CPUID",CPUID);
                    paramBundle.putString("ProduceCutMaID", curProduceCutMaID);
                    paramBundle.putString("MarkDeID",curMarkDeID);
                    paramBundle.putString("MakeState","");
                    intentPullActivity.putExtras(paramBundle);
                    startActivity(intentPullActivity);
                } else {
                    curToast.setText("请先扫描马克合约条码！");
                    curToast.show();
                }
                break;
            case R.id.btn_PullEdit:
                if ((curProduceCutMaID!=null)&&(curProduceCutMaID.length()>0)){
                    Intent intentPullActivity=new Intent(getContext(),PullActivity.class);
                    //简单参数通过Intent的Extras传递;
                    paramBundle.clear();
                    paramBundle.putString("LoginUserNo", curLoginUserNo);
                    paramBundle.putString("LoginUser",curLoginUser);
                    paramBundle.putString("CurDepartNo",curDepartNo);
                    paramBundle.putString("CurDepart",curDepart);
                    paramBundle.putString("ServiceUrl",curServiceUrl);
                    paramBundle.putString("ServiceDir",curServiceDir);
                    paramBundle.putString("CPUID",CPUID);
                    paramBundle.putString("ProduceCutMaID", curProduceCutMaID);
                    paramBundle.putString("MarkDeID",curMarkDeID);
                    paramBundle.putString("MakeState",curMakeState);
                    intentPullActivity.putExtras(paramBundle);
                    startActivity(intentPullActivity);
                } else {
                    curToast.setText("请先扫描马克合约条码并选择需编辑的床次！");
                    curToast.show();
                }
                break;
            case R.id.btn_editBarcode:
                edt_barcode.setInputType(InputType.TYPE_CLASS_TEXT);
                //1.得到InputMethodManager对象
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
                //2.调用showSoftInput方法显示软键盘，其中view为聚焦的view组件
                imm.showSoftInput(edt_barcode,InputMethodManager.SHOW_FORCED);
                break;

            default:
                break;
        }
    }


    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData(final String SQLComm) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try{
                    JSONArray jsonArray  = DBUtil.QuerySQL2JSON(SQLComm);
                    //往界面转数据;
                    if ((jsonArray!=null)&&(jsonArray.length()>0)) {
                        if (jsonArray.optJSONObject(0).has("OPERTYPE")==true) {
                            System.out.println("OPERTYPE:" + jsonArray.optJSONObject(0).getString("OPERTYPE"));
                            if (jsonArray.optJSONObject(0).getString("OPERTYPE").equals("SCANBARCODE")) {
                                headJSON= jsonArray;
                                //刷新界面;
                                Message msg = new Message();
                                msg.what = MSG_SCANBARCODE;
                                fragmentHandler.removeMessages(msg.what);
                                fragmentHandler.sendMessage(msg);
                            }
                            if (jsonArray.optJSONObject(0).getString("OPERTYPE").equals("QUERYMARK")) {
                                detailJSON=jsonArray;
                                //显示预排单;
                                Message msg = new Message();
                                msg.what = MSG_QUERYMARK;
                                fragmentHandler.removeMessages(msg.what);
                                fragmentHandler.sendMessage(msg);
                            }
                            if (jsonArray.optJSONObject(0).getString("OPERTYPE").equals("CUTLIST")) {
                                cutlistJson=jsonArray;
                                //显示预排单;
                                Message msg = new Message();
                                msg.what = MSG_CUTLIST;
                                fragmentHandler.removeMessages(msg.what);
                                fragmentHandler.sendMessage(msg);
                            }
                        }
                        if (jsonArray.optJSONObject(0).has("SQLExcept") == true) {
                            Message message = new Message();
                            message.what = MSG_ERRMSG;
                            Bundle bundle = new Bundle();
                            bundle.putString("Hint", jsonArray.optJSONObject(0).getString("SQLMessage"));
                            message.setData(bundle);
                            fragmentHandler.sendMessage(message);
                        }

                    } else {
                        Message message = new Message();
                        message.what = MSG_ERRMSG;
                        Bundle bundle = new Bundle();
                        bundle.putString("Hint", "未返回任何数据!");
                        fragmentHandler.removeMessages(message.what);
                        fragmentHandler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message message = new Message();
                    message.what = MSG_ERRMSG;
                    Bundle bundle = new Bundle();
                    bundle.putString("Hint", e.getMessage().toString());
                    message.setData(bundle);
                    fragmentHandler.sendMessage(message);
                }

            }

        };
        new Thread(run).start();

    }


    public  String TransValue(String params)
    {
        if (params.toUpperCase().equals("NULL")||params.equals("0")||params.equals("0.0"))
            return "";
        else return params;
    }


    //订单表格适配器;
    public final class MarkListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public MarkListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return detailJSON.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return detailJSON.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) convertView = inflater.inflate(R.layout.fragment_a_listhead, null, false);
            int pullVatListWidth=parent.getWidth();
            convertView.setLayoutParams(new ListView.LayoutParams((int)pullVatListWidth,(int)(38)));
            int color = getResources().getColor(R.color.text_black);
            String tmpValue;
            try {
                Log.d("msg","getView()----------------------------------------------------->"+position);
                //控制是否需要显示对应的尺码;
                if (detailJSON.optJSONObject(position).has("ConColor")==true) {
                    tmpValue = detailJSON.optJSONObject(position).getString("ConColor").toString();
                    if (tmpValue.equals("1"))
                        color = getResources().getColor(R.color.text_blue);
                    if (tmpValue.equals("2"))
                        color = getResources().getColor(R.color.text_red);
                    if (tmpValue.equals("3"))
                        color = getResources().getColor(R.color.text_gray);
                    if (tmpValue.equals("0"))
                        color = getResources().getColor(R.color.text_black);
                }
                TextView tv = convertView.findViewById(R.id.txt_item_Color);// 显示文字
                if (detailJSON.optJSONObject(position).has("Color")==true)
                    tv.setText(detailJSON.optJSONObject(position).get("Color").toString());
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_ColorNo);// 显示文字
                if (detailJSON.optJSONObject(position).has("ColorNo")==true)
                    tv.setText(detailJSON.optJSONObject(position).get("ColorNo").toString());
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Pertain);// 显示文字
                if (detailJSON.optJSONObject(position).has("Pertain")==true)
                    tv.setText(detailJSON.optJSONObject(position).get("Pertain").toString());
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_Size1);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size1")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size1").toString()));
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_Size2);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size2")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size2").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size3);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size3")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size3").toString()));
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_Size4);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size4")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size4").toString()));
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_Size5);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size5")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size5").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size6);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size6")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size6").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size7);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size7")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size7").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size8);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size8")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size8").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size9);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size9")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size9").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size10);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size10")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size10").toString()));
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_Size11);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size11")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size11").toString()));
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_Size12);// 显示文字
                if (detailJSON.optJSONObject(position).has("Size12")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("Size12").toString()));
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_TotalPiece);// 显示文字
                if (detailJSON.optJSONObject(position).has("TotalPiece")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("TotalPiece").toString()));
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_LevelNum);// 显示文字
                if (detailJSON.optJSONObject(position).has("LevelNum")==true)
                    tv.setText(TransValue(detailJSON.optJSONObject(position).get("LevelNum").toString()));
                tv.setTextColor(color);
            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
            Log.d("msg","MarkListAdapter_position:"+position);
            return convertView;
        }
    }

    //订单表格适配器;
    public final class CutListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public CutListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return cutlistJson.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return cutlistJson.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) convertView = inflater.inflate(R.layout.fragment_a_cutlisthead, null, false);
            int pullVatListWidth=parent.getWidth();
            convertView.setLayoutParams(new ListView.LayoutParams((int)pullVatListWidth,(int)(38)));
            int color = getResources().getColor(R.color.text_black);
            String tmpValue;
            try {
                Log.d("msg","getView()----------------------------------------------------->"+position);
                //控制是否需要显示对应的尺码;
                if (cutlistJson.optJSONObject(position).has("ConColor")==true) {
                    tmpValue = cutlistJson.optJSONObject(position).getString("ConColor").toString();
                    if (tmpValue.equals("1"))
                        color = getResources().getColor(R.color.text_blue);
                    if (tmpValue.equals("2"))
                        color = getResources().getColor(R.color.text_red);
                    if (tmpValue.equals("3"))
                        color = getResources().getColor(R.color.text_gray);
                    if (tmpValue.equals("0"))
                        color = getResources().getColor(R.color.text_black);
                }
                TextView tv = convertView.findViewById(R.id.txt_item_CutDate);// 显示文字
                if (cutlistJson.optJSONObject(position).has("CutDate")==true)
                    tv.setText(cutlistJson.optJSONObject(position).get("CutDate").toString());
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_CutInv);// 显示文字
                if (cutlistJson.optJSONObject(position).has("CutInv")==true)
                    tv.setText(TransValue(cutlistJson.optJSONObject(position).get("CutInv").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_BedNo);// 显示文字
                if (cutlistJson.optJSONObject(position).has("BedNo")==true)
                    tv.setText(TransValue(cutlistJson.optJSONObject(position).get("BedNo").toString()));
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_LevelNum);// 显示文字
                if (cutlistJson.optJSONObject(position).has("LevelNum")==true)
                    tv.setText(TransValue(cutlistJson.optJSONObject(position).get("LevelNum").toString()));
                tv.setTextColor(color);
                tv = convertView.findViewById(R.id.txt_item_Number);// 显示文字
                if (cutlistJson.optJSONObject(position).has("Number")==true)
                    tv.setText(TransValue(cutlistJson.optJSONObject(position).get("Number").toString()));
                tv.setTextColor(color);

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
            Log.d("msg","CutListAdapter_position:"+position);
            return convertView;
        }
    }

    //清除界面显示内容
    private void clearFormText(){
        txt_HintInfo.setText("");
        txt_OrderNo.setText("");
        txt_StyleNo.setText("");
        txt_StyleSort.setText("");
        txt_HintInfo.setText("");
    }

    //填充界面显示内容;
    private void fillFormtext(){
        if (headJSON.length()>0)
        {
            try {
                if (headJSON.getJSONObject(0).has("ProduceMasterID")==true) curProduceMasterID=headJSON.getJSONObject(0).getString("ProduceMasterID");
                if (headJSON.getJSONObject(0).has("OrderNo")==true) txt_OrderNo.setText(headJSON.getJSONObject(0).getString("OrderNo"));
                if (headJSON.getJSONObject(0).has("StyleNo")==true) txt_StyleNo.setText(headJSON.getJSONObject(0).getString("StyleNo"));
                if (headJSON.getJSONObject(0).has("StyleSort")==true) txt_StyleSort.setText(headJSON.getJSONObject(0).getString("StyleSort"));
                if (headJSON.getJSONObject(0).has("HintInfo")==true) txt_HintInfo.setText(headJSON.getJSONObject(0).getString("HintInfo"));
                if (headJSON.getJSONObject(0).has("Size1")==true) itemSize1.setText(headJSON.getJSONObject(0).getString("Size1"));else itemSize1.setText("--");
                if (headJSON.getJSONObject(0).has("Size2")==true) itemSize2.setText(headJSON.getJSONObject(0).getString("Size2"));else itemSize2.setText("--");
                if (headJSON.getJSONObject(0).has("Size3")==true) itemSize3.setText(headJSON.getJSONObject(0).getString("Size3"));else itemSize3.setText("--");
                if (headJSON.getJSONObject(0).has("Size4")==true) itemSize4.setText(headJSON.getJSONObject(0).getString("Size4"));else itemSize4.setText("--");
                if (headJSON.getJSONObject(0).has("Size5")==true) itemSize5.setText(headJSON.getJSONObject(0).getString("Size5"));else itemSize5.setText("--");
                if (headJSON.getJSONObject(0).has("Size6")==true) itemSize6.setText(headJSON.getJSONObject(0).getString("Size6"));else itemSize6.setText("--");
                if (headJSON.getJSONObject(0).has("Size7")==true) itemSize7.setText(headJSON.getJSONObject(0).getString("Size7"));else itemSize7.setText("--");
                if (headJSON.getJSONObject(0).has("Size8")==true) itemSize8.setText(headJSON.getJSONObject(0).getString("Size8"));else itemSize8.setText("--");
                if (headJSON.getJSONObject(0).has("Size9")==true) itemSize9.setText(headJSON.getJSONObject(0).getString("Size9"));else itemSize9.setText("--");
                if (headJSON.getJSONObject(0).has("Size10")==true) itemSize10.setText(headJSON.getJSONObject(0).getString("Size10"));else itemSize10.setText("--");
                if (headJSON.getJSONObject(0).has("Size11")==true) itemSize11.setText(headJSON.getJSONObject(0).getString("Size11"));else itemSize11.setText("--");
                if (headJSON.getJSONObject(0).has("Size12")==true) itemSize12.setText(headJSON.getJSONObject(0).getString("Size12"));else itemSize12.setText("--");
                if (headJSON.getJSONObject(0).has("ErrInfo")==true)
                {
                    AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                            .setTitle("系统提示").setMessage(headJSON.getJSONObject(0).getString("ErrInfo"))
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    return;
                                }
                            }).create(); // 创建对话框
                    alertDialog.show(); // 显示对话框
                }
                if (txt_OrderNo.getText().length()>0) queryMarkData(txt_OrderNo.getText().toString(),txt_StyleNo.getText().toString(),txt_StyleSort.getText().toString());

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


    }




    @Override
    public void onStart() {
        super.onStart();
        //设置焦点
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.Start");
        setEditBarcodeFocus();
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.End");
    }
}
